/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.vlivekpipreort;

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author GiangLT
 */
public class RawKpiFileReader {

    public static void main(String[] args) {
        try {
            new RawKpiFileReader().doProcess();
        } catch (ParseException ex) {
            Logger.getLogger(RawKpiFileReader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void doProcess() throws ParseException {
        FileReader fileReader = null;
        try {
            File input = new File("F:\\Projects\\Livescreen\\dump-analysis\\Hanam_Ninh-Binh\\HaNam-NinhBinh-2014-03-05.csv");
            fileReader = new FileReader(input);
            BufferedReader br = new BufferedReader(fileReader);

            int currentHour = 6;
            String currentBscId = "";
            DateFormat df = new SimpleDateFormat("dd-MM-yy HH:mm");
            String line;

            List<HourlyCellKpi> hourlyCellKpis = Lists.newArrayList();
            Map<String, HourlyCellKpi> cellKpiMap = Maps.newHashMap();
            while ((line = br.readLine()) != null) {
                List<String> tokens = Splitter.on(',').splitToList(line);
                String cellList = tokens.get(8);
                String successCellList = tokens.get(9);
                String failureCellList = tokens.get(10);

                Integer totalCount = Integer.valueOf(tokens.get(11));
                Integer successCount = Integer.valueOf(tokens.get(12));
                Integer failureCount = Integer.valueOf(tokens.get(13)); //=totalCount

                List<String> cells = parseCellList(cellList);
                List<String> failureCells = Lists.newArrayList();
                List<String> successCells = Lists.newArrayList();
                if (failureCount > 0) {
                    if (failureCellList.trim().isEmpty()) {
                        failureCells = Lists.newArrayList(cells);
                    } else {
                        failureCells = parseCellList(failureCellList);
                    }
                }

                if (successCount > 0) {
                    successCells = parseCellList(successCellList);
                }

                String bscId = tokens.get(15);
                String startTime = tokens.get(22);

                Date parsedTime = df.parse(startTime);
                int parsedHour = parsedTime.getHours();

                if (parsedHour != currentHour) {
                    currentHour = parsedHour;
                    hourlyCellKpis.addAll(cellKpiMap.values());
                    cellKpiMap = Maps.newHashMap();
                }

                if (!bscId.equals(currentBscId)) {
                    currentBscId = bscId;
                    hourlyCellKpis.addAll(cellKpiMap.values());
                    cellKpiMap = Maps.newHashMap();
                }

                if (!successCells.isEmpty()) {
                    for (String cell : successCells) {
                        if (cell.equals(currentBscId)){
                            continue;
                        }
                        HourlyCellKpi cellKpi = cellKpiMap.get(cell);
                        if (cellKpi != null) {
                            cellKpi.setSuccessMessage(cellKpi.getSuccessMessage() + 1);
                            cellKpi.setTotalMessage(cellKpi.getTotalMessage() + 1);
                        } else {
                            cellKpi = new HourlyCellKpi();
                            cellKpi.setBscId(currentBscId);
                            cellKpi.setCellId(cell);
                            cellKpi.setHour(currentHour);
                            cellKpi.setSuccessMessage(1);
                            cellKpi.setFaliedMessage(0);
                            cellKpi.setTotalMessage(1);
                            cellKpiMap.put(cell, cellKpi);
                        }
                    }
                }

                if (!failureCells.isEmpty()) {
                    for (String cell : failureCells) {
                        if (cell.equals(currentBscId)){
                            continue;
                        }
                        HourlyCellKpi cellKpi = cellKpiMap.get(cell);
                        if (cellKpi != null) {
                            cellKpi.setFaliedMessage(cellKpi.getFaliedMessage() + 1);
                            cellKpi.setTotalMessage(cellKpi.getTotalMessage() + 1);
                        } else {
                            cellKpi = new HourlyCellKpi();
                            cellKpi.setBscId(currentBscId);
                            cellKpi.setCellId(cell);
                            cellKpi.setHour(currentHour);
                            cellKpi.setFaliedMessage(1);
                            cellKpi.setSuccessMessage(0);
                            cellKpi.setTotalMessage(1);
                            cellKpiMap.put(cell, cellKpi);
                        }
                    }
                }

            }
            br.close();
            for (HourlyCellKpi hourlyCellKpi : hourlyCellKpis) {
                Float kpi = (float) hourlyCellKpi.getSuccessMessage() / (float) hourlyCellKpi.getTotalMessage();
                hourlyCellKpi.setKpi(kpi * 100);
                System.out.println(hourlyCellKpi);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(RawKpiFileReader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(RawKpiFileReader.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            IOUtils.closeQuietly(fileReader);
        }
    }

    private List<String> parseCellList(String cellList) {
        List<String> splitted = Splitter.on(CharMatcher.anyOf("|:")).splitToList(cellList);
        List<String> result = Lists.newArrayListWithCapacity(splitted.size());
        for (String string : splitted) {
            string = Splitter.on("#").splitToList(string).get(0);
            result.add(string);
        }
        return result;
    }
}
